package dados.persistencias;

import java.util.List;

import javax.persistence.TypedQuery;

import negocio.erro.DaoException;
import dados.geral.DAOGenerico;
import dados.interfaces.IDAOCampus;
import basica.administrativo.Campus;
import basica.administrativo.Pessoa;

public class DAOCampus extends DAOGenerico<Campus> implements IDAOCampus {

	@Override
	public List<Campus> pesquisarCampusPorCNPJ(String cnpj) throws DaoException {
		TypedQuery<Campus> query = entityManager.createQuery(
				"from Campus camp where camp.cnpj = :cnpj", Campus.class);
		query.setParameter("cnpj", cnpj);
		return query.getResultList();
	}

	@Override
	public List<Campus> pesquisarCampusPorNome(String nome) throws DaoException {
		TypedQuery<Campus> query = entityManager.createQuery(
				"from Campus camp where camp.nome like :nome", Campus.class);
		query.setParameter("nome", "%" + nome + "%");
		return query.getResultList();
	}
	
	@Override
	public List<Campus> pesquisarCampusPorCodigo(Integer codigo) throws DaoException {
		TypedQuery<Campus> query = entityManager.createQuery(
				"from Campus camp where camp.codigo = :codigo", Campus.class);
		query.setParameter("codigo", "%" + codigo + "%");
		return query.getResultList();
	}

	@Override
	public Boolean pesquisarVinculoCampusFuncionario(Integer codigo) throws DaoException {
		TypedQuery<Pessoa> query = entityManager.createQuery("from Pessoa p where p.campus.codigo = :codigo", Pessoa.class);
		query.setParameter("codigo", codigo);
		
		if(query.getResultList().isEmpty()){
			return false;
		}
		
		return true;
	}
	
	
	/*
	 
	 public List<Entidade> consultarTodos() throws DaoException {
		try {
			String sql = "from " + classePersistente.getSimpleName();
			TypedQuery<Entidade> query = entityManager.createQuery(sql, classePersistente);
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	  
	 */

}
